package dao

import (
	"fmt"
	"gorm.io/gorm"
	"lib/id"
	"lib/structs/ams"
	"lib/structs/tables"
	"time"
)

var AssetDao = new(assetDao)

type assetDao struct{}

func (d assetDao) Update(m tables.AmsAsset) (err error) {
	err = DB.Save(&m).Error
	return err
}
func (d assetDao) FindById(id int64) (m tables.AmsAsset, err error) {
	err = DB.Where("id = ?", id).Find(&m).Error
	return
}
func (d assetDao) DeleteById(id int64) (err error) {
	err = DB.Transaction(func(tx *gorm.DB) error {
		err = tx.Where(&tables.AmsAsset{Id: id}).Delete(&tables.AmsAsset{}).Error
		if err != nil {
			return err
		}
		err = tx.Where(&tables.AmsAssetExtend{AssetId: id}).Delete(&tables.AmsAssetExtend{}).Error
		if err != nil {
			return err
		}
		return nil
	})
	return
}
func (d assetDao) Page(in ams.SearchAssetDto) (li []tables.AmsAsset, total int64, err error) {
	tx := DB.Model(&tables.AmsAsset{})
	if in.AssetCode != "" {
		tx.Where("asset_code like ?", fmt.Sprintf("%%%s%%", in.AssetCode))
	}
	if in.LabelCode != "" {
		tx.Where("label_code like ?", fmt.Sprintf("%%%s%%", in.LabelCode))
	}
	if in.Name != "" {
		tx.Where("name like ?", fmt.Sprintf("%%%s%%", in.Name))
	}
	err = tx.Preload("Extend").Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li).Error
	return
}
func (d assetDao) Create(in tables.AmsAsset, ext tables.AmsAssetExtend) (err error) {
	err = DB.Transaction(func(tx *gorm.DB) error {
		in.Id = id.GetSnowId()
		in.CreateAt = time.Now().Unix()
		in.Num = 1
		err = tx.Create(&in).Error
		if err != nil {
			return err
		}
		ext.AssetId = in.Id
		err = tx.Create(&ext).Error
		if err != nil {
			return err
		}
		return nil
	})
	return err
}

func (d assetDao) CountByType(tid int64) (num int64, err error) {
	err = DB.Model(&tables.AmsAsset{}).Where(&tables.AmsAsset{TypeId: tid}).Count(&num).Error
	return
}
func (d assetDao) CountByCategory(tid int64) (num int64, err error) {
	err = DB.Model(&tables.AmsAsset{}).Where(&tables.AmsAsset{CategoryId: tid}).Count(&num).Error
	return
}
func (d assetDao) CreateType(in tables.AmsAssetType) (err error) {
	err = DB.Create(&in).Error
	return err
}

func (d assetDao) DeleteType(typeId int64) (err error) {
	err = DB.Where(&tables.AmsAssetType{Id: typeId}).Delete(&tables.AmsAssetType{}).Error
	return err
}

func (d assetDao) FindTypeList() (li []tables.AmsAssetType, err error) {
	err = DB.Model(&tables.AmsAssetType{}).Find(&li).Error
	return
}

func (d assetDao) CreateCategory(in tables.AmsAssetCategory) (err error) {
	err = DB.Create(&in).Error
	return err
}

func (d assetDao) DeleteCategory(id int64) (err error) {
	err = DB.Where(&tables.AmsAssetCategory{Id: id}).Delete(&tables.AmsAssetCategory{}).Error
	return err
}

func (d assetDao) FindCategoryList() (li []tables.AmsAssetCategory, err error) {
	err = DB.Model(&tables.AmsAssetCategory{}).Find(&li).Error
	return
}

// 耗材操作
func (d assetDao) AssetConsList() (li []ams.AssetConsListDto, err error) {
	err = DB.Raw("select id as asset_id,name,description,num  from ams_asset where type_id =3").Find(&li).Error
	return
}
func (d assetDao) PageForCons(in ams.SearchAssetDto) (li []tables.AmsAsset, total int64, err error) {
	tx := DB.Model(&tables.AmsAsset{})
	tx.Where("type_id = 3 ") // 耗材
	if in.Name != "" {
		tx.Where("name like ?", fmt.Sprintf("%%%s%%", in.Name))
	}
	err = tx.Preload("Extend").Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li).Error
	return
}
func (d assetDao) CreateConsLog(in tables.AmsAssetConsLog) (err error) {
	in.Id = id.GetSnowId()
	in.CreateAt = time.Now().Unix()
	err = DB.Create(&in).Error
	return err
}
func (d assetDao) PageForConsLog(in ams.SearchAssetConsDto) (li []tables.AmsAssetConsLog, total int64, err error) {
	tx := DB.Model(&tables.AmsAssetConsLog{})
	if in.AssetId != 0 {
		tx.Where("asset_id = ? ", in.AssetId)
	}
	if in.OperationType != "" {
		tx.Where("operation_type = ?", in.OperationType)
	}
	if in.Applicant != "" {
		tx.Where("applicant like ?", fmt.Sprintf("%%%s%%", in.Applicant))
	}
	err = tx.Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li).Error
	return
}
